const router = require('koa-router')()
const JWT = require('jsonwebtoken');
const Mock = require('mockjs')
const { list } = Mock.mock({
  "list|100": [{
    "name": "@cname",
    "state": "@boolean",
    "createTime": "@date",
    "tip": "@cword(10, 20)",
    "id": "@id"
  }]
})
const userList = [{
  "id": "1",
  "nickname": '小猪',
  "username": 'admin',
  "password": 'admin',
  "avator": "https://robohash.org/123?set=set5",
  "role": 'admin'
},{
  "id": "2",
  "nickname": '小狗',
  "username": 'user',
  "password": 'user',
  "avator": "https://robohash.org/34567654?set=set5",
  "role": 'user'
}]

router.post('/login', ctx => {
  const { username, password } = ctx.request.body;
  const item = userList.find(v => v.username === username && v.password === password);

  if(item) {
    ctx.body = {
      code: 200,
      data: {
        userInfo: item,
        token: JWT.sign(item, '666666')
      },
      message: '登录成功， 欢迎' + item.nickname
    }
  } else {
    ctx.body = {
      code: 400,
      message: '账户密码错误， 请重新输入'
    }
  }
 
})

router.delete()

router.get('/list', ctx => {
  ctx.body = {
    code: 200,
    data: list
  }
})

module.exports = router
